import { loginOut, loginCheck } from "@/api";
import { mapGetters } from "vuex";
import { getOauth2Info, removeOauth2Info } from "@/utils/auth.js";

export default {
  computed: {
    siteTypesLength() {
      return this.siteTypes.length;
    },
    ...mapGetters(["userInfo", "siteTypes"]),
  },
  created() {
    this.judgeLogin();
  },
  methods: {
    //判断登录状态
    judgeLogin() {
      loginCheck().then((res) => {
        const oauth2Info = getOauth2Info();
        if (res.code === "-1" || !oauth2Info) {
          removeOauth2Info();
          this.$router.push({ name: "login" });
        }
      });
    },
    jumpUrl(url, type) {
      const userId = this.userInfo.userId || "";
      let path = "";
      if (url !== undefined) {
        if (type === "manager") {
          path = window.location.origin + "/oauth2" + url + "/" + userId;
        } else {
          path = window.location.origin + "/oauth2" + url;
        }
        var host = window.location.hostname;
        var protocol = document.location.protocol;
        var newPath = path + "?prefixUrl=" + protocol + "//" + host;
        window.open(newPath, "_blank");
      }
    },
    // 退出
    handleLogout() {
      loginOut({ json_response: true }).then(() => {
        this.$message({
          message: "退出成功",
          type: "success",
          center: true,
          duration: 1000,
        });
        removeOauth2Info();
        this.$router.push({ name: "login" });
      });
    },
  },
};
